//-----------------------------------------------------------------------
// <copyright file="a_ShipMethodDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ShipMethodDAOBase
    {
        #region Common methods
        public virtual a_ShipMethod Createa_ShipMethodFromReader(IDataReader reader)
        {
            a_ShipMethod item = new a_ShipMethod();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ShipMethodID"))) item.ShipMethodID = (int)reader["ShipMethodID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipServiceID"))) item.ShipServiceID = (int)reader["ShipServiceID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ShipQuotePercentage"))) item.ShipQuotePercentage = (double)reader["ShipQuotePercentage"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDefault"))) item.IsDefault = (bool)reader["IsDefault"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_ShipMethod methods
            
        public virtual void Createa_ShipMethod(a_ShipMethod a_ShipMethod)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipMethods_ins");
                
                database.AddInParameter(dbCommand, "@ShipServiceID", DbType.Int32, a_ShipMethod.ShipServiceID);
                database.AddInParameter(dbCommand, "@ShipQuotePercentage", DbType.Double, a_ShipMethod.ShipQuotePercentage);
                database.AddInParameter(dbCommand, "@IsDefault", DbType.Boolean, a_ShipMethod.IsDefault);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShipMethod.DomainID);
                database.AddOutParameter(dbCommand, "@ShipMethodID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_ShipMethod.ShipMethodID = (int)database.GetParameterValue(dbCommand, "@ShipMethodID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_ShipMethod methods
        
        public virtual void Updatea_ShipMethod(a_ShipMethod a_ShipMethod)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipMethods_upd");            
                
                database.AddInParameter(dbCommand, "@ShipMethodID", DbType.Int32, a_ShipMethod.ShipMethodID);
                database.AddInParameter(dbCommand, "@ShipServiceID", DbType.Int32, a_ShipMethod.ShipServiceID);
                database.AddInParameter(dbCommand, "@ShipQuotePercentage", DbType.Double, a_ShipMethod.ShipQuotePercentage);
                database.AddInParameter(dbCommand, "@IsDefault", DbType.Boolean, a_ShipMethod.IsDefault);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_ShipMethod.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_ShipMethod methods
        public virtual void Deletea_ShipMethod(int shipMethodID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipMethods_del");
                
                database.AddInParameter(dbCommand, "@ShipMethodID", DbType.Int32, shipMethodID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_ShipMethod methods
        
        public virtual a_ShipMethod Geta_ShipMethod(int shipMethodID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipMethods_sel");
                
                database.AddInParameter(dbCommand, "@ShipMethodID", DbType.Int32, shipMethodID);
                
                a_ShipMethod a_ShipMethod =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_ShipMethod = Createa_ShipMethodFromReader(reader);
                        reader.Close();
                    }
                }
                return a_ShipMethod;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ShipMethodList methods
        public virtual a_ShipMethodCollection Geta_ShipMethodList(a_ShipMethodColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ShipMethods_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ShipMethodCollection a_ShipMethodCollection = new a_ShipMethodCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_ShipMethod a_ShipMethod = Createa_ShipMethodFromReader(reader);
                        a_ShipMethodCollection.Add(a_ShipMethod);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ShipMethodCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ShipMethodCollection Geta_ShipMethodList(a_ShipMethodColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ShipMethodList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}